﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Media;
using System.Windows.Media.Effects;

namespace Hugo
{
	public partial class SlideIconCanvas : Canvas
	{
		static int index;
        Image imageSelected;

        public delegate void CarouselIndexChangedEventHandler(object o, CarouselIndexChangedEventArgs e);

        public class CarouselIndexChangedEventArgs : EventArgs
        {
            public readonly int newIndex;

            public CarouselIndexChangedEventArgs(int num)
            {
                newIndex = num;
            }
        }

        public interface CarouselIndexChangedEventListener
        {
            void indexChanged(object o, CarouselIndexChangedEventArgs e);
        }

        public event CarouselIndexChangedEventHandler IndexChanged;

        const int horizontal_padding = 50;

        public SlideIconCanvas(CarouselIndexChangedEventListener el)
		{
			this.InitializeComponent();
            IndexChanged += new CarouselIndexChangedEventHandler(el.indexChanged);
            path.Margin = new Thickness(GlobalSettings.EffectiveScreenMiddleX - path.Width / 2 - horizontal_padding, GlobalSettings.EffectiveScreenMiddleY, 0, 0);
        }

        public void setUIOptions(List<UIOption> ops)
        {
            List <UIOption> options = ops;
            this.pathListBox.ItemsSource = getImages(options);
            this.pathListBox.StartItemIndex = 0;
            this.pathListBox.SelectedIndex = 0;
            InitializeImages();
        }

        private List<Image> getImages(List<UIOption> options)
        {
            List<Image> imgs = new List<Image>();
            foreach (UIOption o in options) {
                imgs.Add(o.getIconImage());
            }
            return imgs;
        }

        List<Image> images;

        public List<Image> getImages()
        {
            return images;
        }

		protected void InitializeImages()
		{
            images = new List<Image>();
            //images properties
            foreach (Image image in pathListBox.Items)
            {
                image.Width = double.NaN;
                image.Height = double.NaN;
                image.MaxWidth = 75;
                image.MaxHeight = 75;
                //image.Effect = new DropShadowEffect();
                image.Stretch = Stretch.Uniform;
                image.MouseEnter += new MouseEventHandler(image_MouseEnter);
                image.MouseLeave += new MouseEventHandler(image_MouseLeave);
                images.Add(image);
            }

			index = 0;
			imageSelected = pathListBox.Items[index] as Image;

            Sounds.playSound(Sounds.Sound.Navigate);
		}

        private void image_MouseEnter(object sender, MouseEventArgs e)
        {
            Image image = sender as Image;
            if (pathListBox.Items[index] as Image == image)
            {
                /*image.Effect = new DropShadowEffect
                {
                    Color = new Color { A = 50, R = 255, G = 255, B = 255 },
                    BlurRadius = 5,
                    ShadowDepth = 0,
                };*/
            }
        }

        private void image_MouseLeave(object sender, MouseEventArgs e)
        {
            Image image = sender as Image;
            //image.Effect = new DropShadowEffect();
        }

        private void backImage_MouseUp(object sender, MouseButtonEventArgs e)
        {
            //NavigationController.NavigateTo(new HomePage());
        }

        /**
         * After
         */

        internal void setSelectedIndex(int index)
        {
            pathListBox.SelectedIndex = index;
            CarouselIndexChangedEventArgs e1 = new CarouselIndexChangedEventArgs(index);
            OnIndexChanged(e1);
        }

        public void OnIndexChanged(CarouselIndexChangedEventArgs e)
        {
            if (IndexChanged != null)
                IndexChanged(new object(), e);
        }    

        internal int getSelectedIndex()
        {
            return pathListBox.SelectedIndex;
        }
    }
}